#include<bits/stdc++.h>
using namespace std;
char data[100000000];
string rawStr, encodedStr, decodedStr;
char base[64]; 
char table[256];
void init() 
{ 
    for (int i = 0; i < 26; i++)
        base[i] = 'A' + i; 
    for (int i = 0; i < 26; i++)
        base[26 + i] = 'a' + i; 
    for (int i = 0; i < 10; i++) 
        base[52 + i] = '0' + i; 
    base[62] = '+';
    base[63] = '/'; 
    for (int i = 0; i < 256; i++)
        table[i] = 0xff; 
    for (int i = 0; i < 64; i++)
        table[base[i]] = i; 
    table['='] = 0; 
}
string encode(char rawData[], int len)
{
    string ret = "";
    int i;
    for(i = 0; i + 2 < len; i += 3)
    {
        ret += base[(rawData[i] >> 2) & 0x3f];
        ret += base[(rawData[i] & 0x03) << 4 | (rawData[i+1] >> 4) & 0x0f];
        ret += base[(rawData[i+1] & 0x0f) << 2 | (rawData[i+2] >> 6) & 0x03];
        ret += base[rawData[i+2] & 0x3f];
    }
    if(i == len)
    {
        ret += '=';
    }
    else if(i + 1 == len)
    {
        ret += base[(rawData[i] >> 2) & 0x3f];
        ret += base[(rawData[i] & 0x03) << 4];
        ret += '=';
    }
    else if(i + 2 == len)
    {
        ret += base[(rawData[i] >> 2) & 0x3f];
        ret += base[(rawData[i] & 0x03) << 4 | (rawData[i+1] >> 4) & 0x0f];
        ret += base[(rawData[i+1] & 0x0f) << 2];
        ret += '=';
    }
    return ret;
}
string decode(string str) 
{ 
    string ret = ""; 
    int i; 
    for (i = 0; i < str.size(); i += 4) { 
        if(str[i] == '=')
            break;
        ret += table[str[i]] << 2 | table[str[i + 1]] >> 4;     
        if(str[i+1] == '=')
            break;
        ret += (table[str[i + 1]] & 0x0f) << 4 | table[str[i + 2]] >> 2; 
        if (str[i + 2] == '=')
            break;
        ret += table[str[i + 2]] << 6 | table[str[i + 3]];
    } 
    return ret; 
}

int main()
{
    init();
    FILE *fr = fopen("raw.jpg", "rb");
    FILE *fw = fopen("encoded.txt", "w"); 
    fseek(fr, 0, SEEK_END);
    int size = ftell(fr);
    rewind(fr);
    fread(data,1,size,fr);
    encodedStr = encode(data, size);
    fprintf(fw, "%s", encodedStr.c_str());
    cout << "Encoded string has been saved in encoded.txt" << endl;
    fclose(fr);
    fclose(fw);
    return 0;
}
